home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / music4c.sit / Music4C Folder / SFConvert folder / SFConvertInterface.c < prev    next >
C/C++ Source or Header  |  1990-06-25  |  13KB  |  615 lines

  1.  
  2. /*
  3. *  Converts soundfiles between various formats:
  4. * "interleaved", "AIFF", Sound Designer, and floating point.
  5. *  ⌐ Graeme Gerrard October 1988
  6. */
  7.  
  8. #include    "SFConvert.h"
  9. #include    <stdio.h>
  10. #include    <unix.h>
  11. #include    <string.h>
  12. #include    <math.h>
  13. #include    <SANE.h>
  14. #include    "SDtype.h"
  15.  
  16.  
  17. #define        WNE_TRAP_NUM        0x60
  18. #define        UNIMPL_TRAP_NUM        0x9F
  19. #define        SUSPEND_RESUME_BIT    0x0001
  20. #define        SLEEP                0L
  21. #define        NIL_MOUSE_REGION    0L
  22. #define        RESUMING            1
  23. #define        ACTIVATING            1
  24.  
  25.  
  26. int                IFileOpen;
  27. int                OFileOpen;
  28. long            TotalSamps;
  29. CursHandle        watchCurs;
  30. int                SoundFileType;
  31. long            SampleRate;
  32. long            NumChannels;
  33. double            MaxSample;
  34. double            MinSample;
  35. long            SoundFileDirID;
  36. long            StartUpDirID;
  37. int                SFSaveVRef;
  38. long            fileSize;
  39. int                nrec;
  40. Str255            SoundFileName;
  41. Str255            StartDirName, SFDirectoryName;
  42. Str255            NewSoundFileName;
  43. ioParam            myIOParmBlk;
  44. ioParam            NewParmBlk;
  45. int                SFOUTPUTtype;    
  46. Boolean            SDnoResource;
  47. FILE            *ReportFile;
  48. OSErr            theErr;
  49. Boolean            gWNEImplemented, gDone;
  50. EventRecord        event;
  51. long            RecLength;
  52. SysEnvRec        SysEnvData;
  53.  
  54.  
  55. void    init_process(void);
  56. void    CheckSystem(void);
  57. extern    Boolean    OpenSFRead(void);
  58. Boolean    GetOutputPref(void);
  59. extern    Boolean    OpenSFWrite(void);
  60. Boolean    ConvertFile(void);
  61. void  GetMusic4C_Prefs(void);
  62.  
  63. extern    Boolean    updateNewSFResFile(void);
  64.  
  65. extern    void    DoOSErrorAlert(Str255, Str255);
  66. extern    void    OSError(Str255, Str255);
  67. extern    Boolean    CloseSF(void);
  68. void    MainLoop(void);
  69. ProcPtr     restartProc(void);
  70.  
  71. void    init_mgrs(void);
  72. void    init(void);
  73. void    fill_menus(void);
  74.  
  75. void    identify(void);
  76. void    fill_menus(void);
  77.  
  78. void    HandleEvent(void);
  79. void    do_mouse_down(EventRecord    *);
  80. void    do_menu(long);
  81. void    Finish(void);
  82. extern    void    OutLineButton(DialogPtr, int);
  83. void    FixUp(void);
  84.  
  85.  
  86.  
  87.  
  88. extern    Boolean    FloatToINT16(void);
  89. extern    Boolean    FloatToCHUNKY(void);
  90. extern    Boolean    FloatToSD1(void);
  91. extern    Boolean    FloatToSD2(void);
  92. extern    Boolean    FloatToAIFF(void);
  93.  
  94. extern    Boolean    AIFFToSD(void);
  95. extern    Boolean    AIFFToINT16(void);
  96. extern    Boolean    AIFFToFloat(void);
  97.  
  98. extern    Boolean    InterleavedToSD1(void);
  99. extern    Boolean    InterleavedToSD2(void);
  100. extern    Boolean    InterleavedToCHUNKY(void);
  101. extern    Boolean    InterleavedToAIFF(void);
  102. extern    Boolean    InterleavedToFloat(void);
  103.  
  104. extern    Boolean    SD1ToAIFF(void);
  105. extern    Boolean    SD2ToAIFF(void);
  106. extern    Boolean    SD2ToFloat(void);
  107. extern    Boolean    SD1ToFloat(void);
  108. extern    Boolean    SD2ToFloat(void);
  109. extern    Boolean    SD2ToCHUNKY(void);
  110. extern    Boolean    SD1ToCHUNKY(void);
  111. extern    Boolean    SD2ToINT16(void);
  112. extern    Boolean    SD1ToINT16(void);
  113.  
  114. extern    Boolean    SetSFDir(Str255, long *);
  115.  
  116.  
  117. main()
  118. {
  119.  
  120.     SetApplLimit(GetApplLimit());
  121.     MaxApplZone();
  122.     MoreMasters();
  123.     init_process();
  124.     CheckSystem();
  125.     if ( OpenSFRead() ) {
  126.         IFileOpen = TRUE;
  127.         if ( GetOutputPref() ) {
  128.             if ( OpenSFWrite() ) {
  129.                 OFileOpen = TRUE;
  130.                 HiliteMenu(0);
  131.                 if ( ConvertFile() )
  132.                     if ( !updateNewSFResFile() ) {
  133.                         DoOSErrorAlert("\pError updating resource fork for new sound file", NIL);
  134.                 }
  135.             }
  136.         }
  137.         if ( IFileOpen || OFileOpen) {
  138.             CloseSF();
  139.             IFileOpen = FALSE;
  140.             OFileOpen = FALSE;
  141.         }
  142.     }
  143.     MainLoop();
  144.     ExitToShell();    
  145. }    /* main */
  146.  
  147. ProcPtr     restartProc()
  148. {
  149.     /*ExitToShell();*/
  150. }
  151.  
  152.  
  153. void    init_process()
  154. {
  155.     init_mgrs();
  156.     init();
  157.     fill_menus();
  158.  
  159. }
  160.  
  161. void    init_mgrs()
  162. {
  163.     InitGraf(&thePort);
  164.     InitFonts();
  165.     FlushEvents(everyEvent, 0 );
  166.     InitWindows();
  167.     InitCursor();
  168.     InitMenus();
  169.     TEInit();
  170.     InitDialogs(restartProc);
  171. }
  172.  
  173. void    init()
  174. {
  175.     GetMusic4C_Prefs();
  176.     IFileOpen = FALSE;
  177.     OFileOpen = FALSE;
  178.     SampleRate = 44100L;
  179.     watchCurs = GetCursor(watchCursor);
  180.     RecLength = (long)(16384);
  181. }
  182.  
  183. void    identify()
  184. {
  185.     DialogPtr    aboutBox;
  186.     EventRecord    event;
  187.     PicHandle    thePic;
  188.     Rect    myRect, tempRect;
  189.     
  190.     
  191.     aboutBox = GetNewDialog(ABOUT_DIALOG, NIL, -1L);
  192.     tempRect.top = aboutBox->portRect.top;
  193.     tempRect.left = aboutBox->portRect.left;
  194.     tempRect.bottom = aboutBox->portRect.bottom;
  195.     tempRect.right = aboutBox->portRect.right;
  196.     tempRect.top = ((screenBits.bounds.bottom - screenBits.bounds.top) - (tempRect.bottom - tempRect.top)) / 2;
  197.     tempRect.left = ((screenBits.bounds.right - screenBits.bounds.left) - (tempRect.right - tempRect.left)) / 2;
  198.     MoveWindow(aboutBox, tempRect.left, tempRect.top, TRUE);
  199.     ParamText("\p", NIL, NIL, NIL);
  200.     ShowWindow(aboutBox);
  201.     DrawDialog(aboutBox);
  202.     SelectWindow(aboutBox);
  203.     SetPort(aboutBox);
  204.     
  205.     
  206.     while( !EventAvail(keyDownMask|autoKeyMask|mDownMask, &event))
  207.         SystemTask();
  208.         
  209.     if ( event.what == mouseDown ) {
  210.         GlobalToLocal(&event.where);
  211.         if ( PtInRect(event.where, &aboutBox->portRect))
  212.             GetNextEvent(mDownMask, &event);
  213.     }
  214.     DisposDialog(aboutBox);
  215. }
  216.  
  217.  
  218.  
  219.  
  220.  
  221. void    fill_menus()
  222. {
  223.     Str255        mess;
  224.     
  225.     
  226.     if (GetResource('MENU', APPLE_MENU)==0) {
  227.         SysBeep(20);
  228.         DoOSErrorAlert("\pcan't open resource file", NIL);
  229.         return;
  230.     }
  231.  
  232.     AppendMenu(GetMenu(APPLE_MENU), CtoPstr("About SFConvert..."));
  233.     AppendMenu(GetMenu(APPLE_MENU),  CtoPstr("(-"));
  234.  
  235.     AddResMenu(GetMenu(APPLE_MENU), 'DRVR');
  236.     InsertMenu(GetMenu(APPLE_MENU), 0);
  237.     
  238.     InsertMenu(GetMenu(FILE_MENU), 0);
  239.     DisableItem(GetMHandle(FILE_MENU), 2);
  240.     
  241.     DrawMenuBar();
  242. }
  243.  
  244.  
  245.  
  246.  
  247. void    MainLoop()
  248. {
  249.     gDone = FALSE;
  250.     gWNEImplemented = ( NGetTrapAddress( WNE_TRAP_NUM, ToolTrap ) !=
  251.                         NGetTrapAddress ( UNIMPL_TRAP_NUM, ToolTrap ) );
  252.     while( gDone == FALSE ) {
  253.         HandleEvent();
  254.     }
  255. }
  256.  
  257.  
  258. void    HandleEvent()
  259. {
  260.     char c;
  261.     Boolean        valid;
  262.     WindowPtr    update_window;
  263. /*    if ( gWNEImplemented )
  264.         WaitNextEvent(everyEvent, &event, SLEEP, NIL_MOUSE_REGION );
  265.     else {
  266. */
  267.         SystemTask();
  268.         valid = GetNextEvent(everyEvent, &event );
  269.     if ( !valid) return;
  270.     switch(event.what) {
  271.         case nullEvent:
  272.             break;
  273.         case mouseDown:
  274.             do_mouse_down(&event);
  275.             break;
  276.         case mouseUp:
  277.             break;
  278.         case keyDown:
  279.         case autoKey:
  280.                 c = event.message & charCodeMask;
  281.                 if(event.modifiers & cmdKey) {
  282.                     if(event.what != autoKey )
  283.                         do_menu(MenuKey(c));
  284.                 }
  285.                 break;
  286.         case keyUp:
  287.             break;
  288.         case updateEvt:
  289.             break;
  290.         case diskEvt:
  291.             break;
  292.         case activateEvt:
  293.             break;
  294.         case networkEvt:
  295.             break;
  296.         case driverEvt:
  297.             break;
  298.         case app1Evt:
  299.             break;
  300.         case app2Evt:
  301.             break;
  302.         case app3Evt:
  303.             break;
  304.         case app4Evt:
  305.             if ( ( event.message & SUSPEND_RESUME_BIT ) == RESUMING )
  306.                 /* resume event */;
  307.             else
  308.                 /* suspend event */
  309.                     ;
  310.             break;
  311.         default:
  312.             break;
  313.     }
  314. }
  315.  
  316.  
  317.  
  318. void    do_mouse_down(event)
  319.     EventRecord    *event;
  320. {
  321.     WindowPtr    windowp;
  322.     int    windowCode = FindWindow(MK_LONG(event->where), &windowp);
  323.  
  324.     switch(windowCode) {
  325.         case inDesk:
  326.             break;
  327.         case inMenuBar:
  328.             do_menu(MenuSelect(event->where));
  329.             break;
  330.         case inSysWindow:
  331.             SystemClick(event, windowp);
  332.             break;
  333.         case inContent:
  334.             break;
  335.         case inDrag:
  336.             break;
  337.         case inGrow:
  338.             break;
  339.         case inGoAway:
  340.             break;
  341.         default:
  342.             break;
  343.     }
  344. }
  345.  
  346.  
  347. void    do_menu(command)
  348.     long    command;
  349. {
  350.     int    menu_id = HiWord(command);
  351.     int    item    = LoWord(command);
  352.     char    item_name[32];
  353.     
  354.     switch(menu_id) {
  355.         case APPLE_MENU:
  356.             if ( item == AboutItem)
  357.                 identify();
  358.             else {
  359.                 GetItem(GetMHandle(menu_id), item, item_name);
  360.                 OpenDeskAcc(item_name);
  361.             }
  362.             break;
  363.         case FILE_MENU:
  364.             switch(item) {
  365.                 case OPEN:
  366.                     if ( OpenSFRead() ) {
  367.                         IFileOpen = TRUE;
  368.                         if ( GetOutputPref() ) {
  369.                             if ( OpenSFWrite() ) {
  370.                                 OFileOpen = TRUE;
  371.                                 HiliteMenu(0);
  372.                                 if ( ConvertFile() )
  373.                                     if ( !updateNewSFResFile() ) {
  374.                                         DoOSErrorAlert("\pError updating resource fork for new sound file", NIL);
  375.                                     }
  376.                             }
  377.                         }
  378.                         if ( IFileOpen || OFileOpen) {
  379.                             CloseSF();
  380.                             IFileOpen = FALSE;
  381.                             OFileOpen = FALSE;
  382.                         }
  383.                     }
  384.                     break;
  385.                 case QUIT:
  386.                     Finish();
  387.                     break;
  388.             }
  389.             break;
  390.     }
  391.     HiliteMenu(0);
  392. }
  393.  
  394.  
  395. Boolean    ConvertFile()
  396. {
  397.     SetCursor(*GetCursor(watchCursor));
  398.     switch ( SoundFileType ) {
  399.         case FLOAT:
  400.             if (SFOUTPUTtype == INT16)
  401.                 if ( FloatToINT16() )
  402.                     return(TRUE);
  403.             if (SFOUTPUTtype == AIFF)
  404.                 if ( FloatToAIFF() )
  405.                     return(TRUE);
  406.             if (SFOUTPUTtype == SD1)
  407.                 if ( FloatToSD1())
  408.                     return(TRUE);
  409.             if (SFOUTPUTtype == SD2)
  410.                 if ( FloatToSD2())
  411.                     return(TRUE);
  412.             break;
  413.         case    SD1:
  414.             if (SFOUTPUTtype == INT16)
  415.                 if ( SD1ToINT16())
  416.                     return(TRUE);
  417.             if (SFOUTPUTtype == AIFF)
  418.                 if ( SD1ToAIFF() )
  419.                     return(TRUE);
  420.             if (SFOUTPUTtype == FLOAT)
  421.                 if ( SD1ToFloat() )
  422.                     return(TRUE);
  423.             break;
  424.         case    SD2:
  425.             if (SFOUTPUTtype == INT16)
  426.                 if ( SD2ToINT16())
  427.                     return(TRUE);
  428.             if (SFOUTPUTtype == AIFF)
  429.                 if ( SD2ToAIFF() )
  430.                     return(TRUE);
  431.             if (SFOUTPUTtype == FLOAT)
  432.                 if ( SD2ToFloat() )
  433.                     return(TRUE);
  434.             break;
  435.         case    INT16:
  436.             if (SFOUTPUTtype == SD1)
  437.                 if ( InterleavedToSD1() )
  438.                     return(TRUE);
  439.             if (SFOUTPUTtype == AIFF)
  440.                 if ( InterleavedToAIFF() )
  441.                     return(TRUE);
  442.             if (SFOUTPUTtype == FLOAT)
  443.                 if ( InterleavedToFloat() )
  444.                     return(TRUE);
  445.             break;
  446.         case    AIFF:
  447.             if (SFOUTPUTtype == SD1)
  448.                 if ( AIFFToSD() )
  449.                     return(TRUE);
  450.             if (SFOUTPUTtype == INT16)
  451.                 if ( AIFFToINT16() )
  452.                     return(TRUE);
  453.             if (SFOUTPUTtype == FLOAT)
  454.                 if ( AIFFToFloat() )
  455.                     return(TRUE);
  456.             break;
  457.         default:
  458.             InitCursor();
  459.             DoOSErrorAlert("\pAIFF,float,16bit or Sound Designer formats only", NIL);
  460.             return(FALSE);
  461.             break;
  462.     }
  463.     InitCursor();
  464.     return(FALSE);
  465. }
  466.  
  467.  
  468.  
  469.  
  470. void    Finish()
  471. {
  472.     ExitToShell();
  473. }
  474.  
  475.  
  476.  
  477.  
  478. Boolean    GetOutputPref()
  479. {
  480.     int            itemHit;
  481.     int            itype;
  482.     Rect            box, tempRect;
  483.     ControlHandle    radHand1, radHand2, radHand3, radHand4;
  484.     DialogPtr        myDialog;
  485.     
  486.     myDialog = GetNewDialog(OUTPUT_FORMAT, FALSE, (WindowPtr) -1);
  487.      
  488.     tempRect.top = myDialog->portRect.top;
  489.     tempRect.left = myDialog->portRect.left;
  490.     tempRect.bottom = myDialog->portRect.bottom;
  491.     tempRect.right = myDialog->portRect.right;
  492.     tempRect.top = ((screenBits.bounds.bottom - screenBits.bounds.top) - (tempRect.bottom - tempRect.top)) / 2;
  493.     tempRect.left = ((screenBits.bounds.right - screenBits.bounds.left) - (tempRect.right - tempRect.left)) / 2;
  494.     MoveWindow(myDialog, tempRect.left, tempRect.top, TRUE);
  495.      
  496.     
  497.     
  498.  
  499.     if ( !myDialog ) {
  500.         return;
  501.     }
  502.  
  503. /* get handles to the radio buttons for run type */    
  504.     GetDItem(myDialog, ILEAVEDitem, &itype, &radHand1, &box);
  505.     GetDItem(myDialog, AIFFitem, &itype, &radHand2, &box);
  506.     GetDItem(myDialog, SDitem, &itype, &radHand3, &box);
  507.     GetDItem(myDialog, FLTitem, &itype, &radHand4, &box);
  508.     
  509.     switch(SoundFileType) {
  510.         case INT16:
  511.             HiliteControl(radHand1, 255); /* INT16 */
  512.             SetCtlValue(radHand1, 0);
  513.             SetCtlValue(radHand2, 0);
  514.             SetCtlValue(radHand3, 0);
  515.             SetCtlValue(radHand4, 1);
  516.             HiliteControl(radHand2, 255); /* AIFF */
  517.             HiliteControl(radHand3, 255); /* SD */
  518.             SFOUTPUTtype = FLOAT;
  519.             break;
  520.         case AIFF:
  521.             HiliteControl(radHand2, 255); /* AIFF */
  522.             SetCtlValue(radHand1, 0);
  523.             SetCtlValue(radHand2, 0);
  524.             SetCtlValue(radHand3, 0);
  525.             SetCtlValue(radHand4, 1);
  526.             HiliteControl(radHand1, 255); /* INT16 */
  527.             HiliteControl(radHand3, 255); /* SD */
  528.             SFOUTPUTtype = FLOAT;
  529.             break;
  530.         case    SD1:
  531.         case    SD2:
  532.             HiliteControl(radHand1, 255); /* INT16 */
  533.             HiliteControl(radHand3, 255); /* SD */
  534.             HiliteControl(radHand2, 255); /* AIFF */
  535.             SetCtlValue(radHand1, 0);
  536.             SetCtlValue(radHand2, 0);
  537.             SetCtlValue(radHand3, 0);
  538.             SetCtlValue(radHand4, 1);
  539.             SFOUTPUTtype = FLOAT;
  540.             break;
  541.         case FLOAT:
  542.             HiliteControl(radHand4, 255); /* FLOAT */
  543.             SetCtlValue(radHand1, 0);
  544.             SetCtlValue(radHand2, 1);
  545.             SetCtlValue(radHand3, 0);
  546.             SetCtlValue(radHand4, 0);
  547.             SFOUTPUTtype = AIFF;
  548.             break;
  549.     }
  550.     ShowWindow(myDialog);
  551.     OutLineButton(myDialog, USERItem);
  552.     SelectWindow(myDialog);
  553.     SetPort(myDialog);
  554.  
  555.     itemHit = 0;
  556.     while ( itemHit != OKButton && itemHit != CancelButton) {
  557.         ModalDialog((ProcPtr)NIL, &itemHit);
  558.         switch (itemHit) {
  559.             case    ILEAVEDitem:
  560.                 SFOUTPUTtype = INT16;
  561.                 SetCtlValue(radHand1, 1);
  562.                 SetCtlValue(radHand2, 0);
  563.                 SetCtlValue(radHand3, 0);
  564.                 SetCtlValue(radHand4, 0);
  565.                 break;
  566.             case    AIFFitem:
  567.                 SFOUTPUTtype = AIFF;
  568.                 SetCtlValue(radHand1, 0);
  569.                 SetCtlValue(radHand2, 1);
  570.                 SetCtlValue(radHand3, 0);
  571.                 SetCtlValue(radHand4, 0);
  572.                 break;
  573.             case    SDitem:
  574.                 SFOUTPUTtype = SD1;
  575.                 SetCtlValue(radHand1, 0);
  576.                 SetCtlValue(radHand2, 0);
  577.                 SetCtlValue(radHand3, 1);
  578.                 SetCtlValue(radHand4, 0);
  579.                 break;
  580.             case    FLTitem:
  581.                 SFOUTPUTtype = FLOAT;
  582.                 SetCtlValue(radHand1, 0);
  583.                 SetCtlValue(radHand2, 0);
  584.                 SetCtlValue(radHand3, 0);
  585.                 SetCtlValue(radHand4, 1);
  586.                 break;
  587.         }
  588.     }
  589.     DisposDialog(myDialog);
  590.     if ( itemHit == OKButton )
  591.         return(TRUE);
  592.     else
  593.         return(FALSE);
  594. }
  595.  
  596.  
  597. void    FixUp()
  598. {
  599.     if ( StartDirName[0] != NIL ) {
  600.         SetSFDir(StartDirName, &StartUpDirID);
  601.     }
  602. }
  603.  
  604.  
  605.  
  606. void    CheckSystem()
  607. {
  608.     
  609.     theErr = SysEnvirons(1, &SysEnvData);
  610.     if (SysEnvData.hasFPU)
  611.         return;
  612.     OSError("\pThis version of SFConvert", 
  613.         "\prequires a floating point unit");
  614.     ExitToShell();
  615. }